package edu.whu.webgis.service.impl;

import edu.whu.webgis.entity.User;
import edu.whu.webgis.entity.VUser;
import edu.whu.webgis.mapper.UserMapper;
import edu.whu.webgis.service.IUserService;
import edu.whu.webgis.utils.ImgUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    public User getUser(String id){
        return userMapper.selectById(id);
    }

    @Override
    public String getAvatar(String id) {
        String avatarName = userMapper.selectAvatar(id);
        return ImgUtil.getBase64Jpg(avatarName);
    }

    @Override
    public List<Map<String, String>> getAllBaseInfo() {
        return userMapper.selectAllBaseInfo();
    }

    @Override
    public void updateBaseInfo(User user) {
        userMapper.updateBaseDetail(user.getId(),user.getSignature(),user.getNickname());
        if(!Objects.equals(user.getAvatar(), "")){
            userMapper.updatePhoto(ImgUtil.save(user.getAvatar()),user.getId());
        }

    }

    @Override
    public VUser getUserDetail(String id) {
        User user = userMapper.selectById(id);
        return new VUser(user);
    }

}
